<?php 

session_start();

if (@$_REQUEST['actionType'] == "add")
{
	@$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] = @$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] + $_REQUEST["quantity"];
	header('Location:favoris.php');
}
elseif (@$_REQUEST['actionType'] == "remove")
{
	@$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] = @$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] - $_REQUEST["quantity"];
	if (@$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] < 0)
	{
		@$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"][$_REQUEST["iid"]]["bouteilles"][$_REQUEST["aid"]]["qty"] = 0;
	}
	header('Location:favoris.php');
}

@include 'include/header.php';
?>

<script type="text/javascript">
	function addBottle(iid, aid, count)
	{
		document.getElementById("iid").value = iid;
		document.getElementById("aid").value = aid;
		document.getElementById("quantity").value = document.getElementById("qty"+count).value;
		document.getElementById("actionType").value = 'add'
		form.submit();
	}
	
	function removeBottle(iid, aid, count)
	{
		document.getElementById("iid").value = iid;
		document.getElementById("aid").value = aid;
		document.getElementById("quantity").value = document.getElementById("qty"+count).value;
		document.getElementById("actionType").value = 'remove'
		form.submit();
	}
	
</script>

<form name="form" action="favoris.php" method="GET">

	<input type="hidden" id="iid" name="iid" value=""> <input type="hidden"
		id="aid" name="aid" value=""> <input type="hidden" id="quantity"
		name="quantity" value=""> <input type="hidden" id="actionType"
		name="actionType" value="">

	<div id="contenu">
		<h1>Favoris</h1>
		<table class="favoris" cellspacing="0" cellpadding="0">
			<tr>
				<?php
				$favorisCount = 0;
				$inventaires = @$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"];
				for ($i=0;$i<count(@$inventaires);$i++)
				{
					$inventaire = $inventaires[$i];
					for ($j=0;$j<count(@$inventaire["bouteilles"]);$j++)
					{
						$alcool = $inventaire["bouteilles"][$j];
						if (@$alcool["favoris"] === true)
						{
							$cote = $alcool['cote'] / 5 * 100;
							$favorisCount++;
							echo
							"
							<td>
							<a href=\"alcool.php?iid=".$i."&aid=".$j."\">
							<div class=\"photo\">
							<img src=\"images/".@$alcool["image"]."\" alt=\"Photo\" />
							</div>
							</a>
							<h2>". $alcool["nom"] ."</h2>
							<p>"
							. @$alcool["type"]
							. "-"
							. @$alcool["provenance"]
							. "("
							. @$alcool["tauxAlcool"]
							. ")"
							."</p>
							<p>"
							. @$alcool["qty"] . " bouteilles
							<a href=\"javascript:addBottle('".$i."', '".$j."', '".$favorisCount."')\" title=\"Ajouter\"><img src=\"images/ajouter.png\" alt=\"Ajouter\" /></a>
							<input id=qty".$favorisCount." type=\"text\" maxlength=\"2\" size=\"2\" value=\"1\"/>
							<a href=\"javascript:removeBottle('".$i."', '".$j."', '".$favorisCount."')\" title=\"Enlever\"><img src=\"images/enlever.png\" alt=\"Enlever\" /></a>
							</p>
							<p>
							<div class=\"classification\">
							<div class=\"cover\"></div>
							<div class=\"progress\" style=\"width: ".$cote."%;\"></div>
							</div>
							</p>
							</td>";
						}
					}
				}
				?>
			</tr>
		</table>

</form>
<h2>Inventaires</h2>
<?php 
$inventaires = @$_SESSION["profils"][$_SESSION["profilActuel"]]["inventaires"];
for ($i=0;$i<count($inventaires);$i++)
{
	echo
	"<p>
	<a href=\"inventaire.php?iid=".$i."\">".$inventaires[$i]["nom"]."</a>
	</p>";
}
?>

<p>
	<a href="ajoutInventaire.php">Ajouter un inventaire</a>
</p>
</div>
</body>

</html>
